//
//  XMMineViewController.m
//  XunMi
//
//  Created by MacBook on 2024/2/18.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMMineViewController.h"
#import "XMMineTableViewCell.h"
#import "XMMineNavRightView.h"
#import "XMMineUserInfoView.h"
#import "XMMineToolsView.h"
#import <SDCycleScrollView/SDCycleScrollView.h>
#import "XMMineSettingViewController.h"
#import "XMBagViewController.h"
#import "XMUserDetailViewController.h"
#import "XMRechargeViewController.h"
#import "XMAgreementViewController.h"
#import "XMSeeMeViewController.h"
#import "XMMyDynamicViewController.h"
#import "XMRealViewController.h"
#import "XMBeautyViewController.h"
#import "XMVideoLevelViewController.h"
#import "XMTaskViewController.h"
#import "XMInviteViewController.h"
#import "XMPointsViewController.h"
#import "XMActiveViewController.h"
#import "XMMemberViewController.h"

@interface XMMineViewController () <SDCycleScrollViewDelegate>
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) XMMineNavRightView *rightView;
@property (nonatomic, strong) XMMineUserInfoView *infoView;
@property (nonatomic, strong) XMMineToolsView *toolsView;
@property (nonatomic, strong) UIImageView *rechargeView;
@property (nonatomic, strong) UILabel *memberTipLa;

@property (nonatomic, strong) SDCycleScrollView *bannerView;
@property (nonatomic, strong) NSMutableArray <XMBannerModel *> *bannerArray;

@end

@implementation XMMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initVariables];
    [self setupSubviews];
    [self viewBlock];
    [self bannerList];
}

- (void)bannerList {
    NSMutableDictionary *dic = @{}.mutableCopy;
    [YHNetworkManager.sharedInstance postWithUrl:@"api/config/bannerList" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        if (tip.success) {
            NSArray *arr = [result safe_objectForKey:@"record"];
            self.bannerArray = [XMBannerModel mj_objectArrayWithKeyValuesArray:arr];
            __block NSMutableArray *imgUrlArray = @[].mutableCopy;
            [self.bannerArray enumerateObjectsUsingBlock:^(XMBannerModel *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
                [imgUrlArray addObject:[NSString xm_stringNoNullWith:obj.cover.file_url]];
            }];
            self.bannerView.imageURLStringsGroup = imgUrlArray;
        }
    }];
}

#pragma mark - 初始化变量
- (void)initVariables {
    [self tableViewRegisterNibWithNibName:kStringFromClass(XMMineTableViewCell)];
}
#pragma mark - setupSubViews
- (void)setupSubviews {
    self.view.backgroundColor = UIColor.whiteColor;
    self.tableView.backgroundColor = UIColor.whiteColor;
    [self.navLeftBtn setImage:nil forState:UIControlStateNormal];
    [self.navLeftBtn setTitle:@"我的" forState:UIControlStateNormal];
    self.navLeftBtn.titleLabel.font = kUIFontBold(19);
    [self.navLeftBtn setTitleColor:k1F1F1F forState:UIControlStateNormal];
    [self.navLeftBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(70, 44));
    }];
    kCornerRadius(self.rightView, 17, 0, UIColor.clearColor);
    [self.navView addSubview:self.rightView];
    [self.rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.navRightBtn);
        make.right.equalTo(self.navRightBtn);
        make.size.mas_equalTo(CGSizeMake(84, 34));
    }];
    
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 18 + 60 + 78 + 68 + 96 + 10)];
    self.headerView.backgroundColor = UIColor.whiteColor;
    [self.headerView addSubview:self.infoView];
    [self.infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.headerView);
        make.top.equalTo(self.headerView).offset(18);
        make.height.mas_equalTo(60);
    }];
    
    [self.headerView addSubview:self.toolsView];
    [self.toolsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.headerView);
        make.top.equalTo(self.infoView.mas_bottom).offset(0);
        make.height.mas_equalTo(78);
    }];
    
    [self.headerView addSubview:self.rechargeView];
    [self.rechargeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kScreenWidth - 28);
        make.centerX.equalTo(self.headerView);
        make.top.equalTo(self.toolsView.mas_bottom).offset(0);
        make.height.mas_equalTo(68);
    }];
    
    self.memberTipLa = [UILabel labelWithText:@"" textFont:12 textColor:kHEX(@"#FBCF8E") frame:CGRectZero];
    
    [self.headerView addSubview:_memberTipLa];
    [self.memberTipLa mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.rechargeView.mas_top).offset(38);
        make.left.equalTo(self.headerView).offset(32);
    }];
    
    [self.headerView addSubview:self.bannerView];
    [self.bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kScreenWidth - 28);
        make.centerX.equalTo(self.headerView);
        make.top.equalTo(self.rechargeView.mas_bottom).offset(10);
        make.height.mas_equalTo(96);
    }];
    self.tableView.tableHeaderView = self.headerView;
}

#pragma mark - viewBlock
- (void)viewBlock {
    XMWeakSelf(self);
    [self.rightView navRightViewMoreBlock:^{
        XMLog(@"more");
    } setBlock:^{
        XMStrongSelf(self);
        XMLog(@"set");
        XMMineSettingViewController *vc = [[XMMineSettingViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    
    self.infoView.editBlock = ^{
        XMStrongSelf(self);
        XMUserDetailViewController *vc = [[XMUserDetailViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    };
    
    self.toolsView.clickBlock = ^(XMMineToolsType type) {
        XMStrongSelf(self);
        switch (type) {
            case XMMineToolsBag:
            {
                XMBagViewController *vc = [[XMBagViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            case XMMineToolsGold:
            {
                [self toRecharge];
            }
                break;
                
            case XMMineToolsScore:
            {
                XMPointsViewController *vc = [[XMPointsViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
                
            default:
                break;
        }
    };
}
#pragma mark - Actions
#pragma mark - Public Methods
#pragma mark - Privacy Methods

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self mineUserInfo];
}

- (void)mineUserInfo {
    NSMutableDictionary *dic = @{
        @"module": @"3"
    }.mutableCopy;
    [YHNetworkManager.sharedInstance postWithUrl:@"api/user/obtain" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        if (tip.success) {
            XMUserModel *model = [XMUserModel mj_objectWithKeyValues:result];
            kUserModel = model;
            if (kUserModel.isMan) {
                self.dataArray = @[
                    @[@{@"img":@"mine_dt", @"name":@"我的动态"},
                      @{@"img":@"mine_sm", @"name":@"实名认证"},
                      @{@"img":@"mine_my", @"name":@"美颜设置"}],
                    
                    @[
                      @{@"img":@"mine_rw", @"name":@"任务中心"},
                      @{@"img":@"mine_yq", @"name":@"邀请好友"},
                      @{@"img":@"mine_kan", @"name":@"谁看过我"}],
                    
                    @[@{@"img":@"mine_bz", @"name":@"隐私政策"},
                      @{@"img":@"mine_xy", @"name":@"用户协议"}]
                ].mutableCopy;
            } else {
                self.dataArray = @[
                    @[@{@"img":@"mine_dt", @"name":@"我的动态"},
                      @{@"img":@"mine_sm", @"name":@"实名认证"},
                      @{@"img":@"mine_my", @"name":@"美颜设置"}],
                    @[
                      @{@"img":@"mine_sp", @"name":@"视频等级"},
                      @{@"img":@"mine_rw", @"name":@"任务中心"},
                      @{@"img":@"mine_yq", @"name":@"邀请好友"},
                      @{@"img":@"mine_kan", @"name":@"谁看过我"}],
                    
                    @[@{@"img":@"mine_bz", @"name":@"隐私政策"},
                      @{@"img":@"mine_xy", @"name":@"用户协议"}]
                ].mutableCopy;
            }
            [self.toolsView reload];
            [self.infoView reload];
            [self.tableView reloadData];
            self.memberTipLa.text = [XMUtil isBlankString:model.member_time] ? @"会员权益多多" : model.member_time;
        }
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataArray[section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 48;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [UIView new];
    view.backgroundColor = k8C8F9DA;
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMMineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kStringFromClass(XMMineTableViewCell)];
    NSArray *arr = [self.dataArray objectAtIndex:indexPath.section];
    NSDictionary *dic = [arr objectAtIndex:indexPath.row];
    
    cell.lineV.hidden = indexPath.row == arr.count - 1;
    cell.imgV.image = kGetImage(dic[@"img"]);
    cell.nameLa.text = dic[@"name"];
    cell.realLa.hidden = YES;
    if ([dic[@"name"] isEqualToString:@"实名认证"]) {
        cell.realLa.hidden = NO;
        if (kUserModel.idcard_verify) {
            cell.realLa.backgroundColor = kHEX(@"#5283F0");
            kCornerRadius(cell.realLa, 12, 0, UIColor.clearColor);
            cell.realLa.text = @"已实名";
            cell.realLa.textColor = UIColor.whiteColor;
        } else {
            cell.realLa.backgroundColor = kHEXA(@"##D8D8D8", 0.2);
            kCornerRadius(cell.realLa, 12, 1, kHEX(@"#DCDCDC"));
            cell.realLa.text = @"未实名";
            cell.realLa.textColor = kHEX(@"#A6A6A6");
        }
    }
    cell.rwjlV.hidden = YES;
    if ([dic[@"name"] isEqualToString:@"任务中心"]) {
        cell.rwjlV.hidden = NO;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *arr = [self.dataArray objectAtIndex:indexPath.section];
    NSDictionary *dic = [arr objectAtIndex:indexPath.row];
    NSString *name = dic[@"name"];
    if ([name isEqualToString:@"用户协议"]) {
        XMWebViewController *vc = [[XMWebViewController alloc] init];
        vc.titleStr = @"用户协议";
        vc.urlStr = kGlobalVariables.appConfig.privacy.serviceAgreement;
        vc.needNav = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if ([name isEqualToString:@"隐私政策"]) {
        XMWebViewController *vc = [[XMWebViewController alloc] init];
        vc.titleStr = @"隐私政策";
        vc.urlStr = kGlobalVariables.appConfig.privacy.privacyAgreement;
        vc.needNav = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
    }
    
    if ([name isEqualToString:@"谁看过我"]) {
        XMSeeMeViewController *vc = [[XMSeeMeViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    //
    if ([name isEqualToString:@"我的动态"]) {
        XMMyDynamicViewController *vc = [[XMMyDynamicViewController alloc] init];
        vc.userId = kUserModel.user_id;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if ([name isEqualToString:@"实名认证"]) {
        XMRealViewController *vc = [[XMRealViewController alloc] initWithNibName:kStringFromClass(XMRealViewController) bundle:nil];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if ([name isEqualToString:@"美颜设置"]) {
        XMBeautyViewController *vc = [[XMBeautyViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if ([name isEqualToString:@"视频等级"]) {
        XMVideoLevelViewController *vc = [[XMVideoLevelViewController alloc] initWithNibName:kStringFromClass(XMVideoLevelViewController) bundle:nil];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if ([name isEqualToString:@"任务中心"]) {
        XMTaskViewController *vc = [[XMTaskViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if ([name isEqualToString:@"邀请好友"]) {
        XMInviteViewController *vc = [[XMInviteViewController alloc] initWithNibName:kStringFromClass(XMInviteViewController) bundle:nil];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if ([name isEqualToString:@"活跃度"]) {
        XMActiveViewController *vc = [[XMActiveViewController alloc] initWithNibName:kStringFromClass(XMActiveViewController) bundle:nil];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

- (XMMineNavRightView *)rightView {
    if (_rightView == nil) {
        _rightView = [XMMineNavRightView viewFromXib];
    }
    return _rightView;
}

- (XMMineUserInfoView *)infoView {
    if (_infoView == nil) {
        _infoView = [XMMineUserInfoView viewFromXib];
    }
    return _infoView;
}

- (XMMineToolsView *)toolsView {
    if (_toolsView == nil) {
        _toolsView = [XMMineToolsView viewFromXib];
    }
    return _toolsView;
}

- (UIImageView *)rechargeView {
    if (_rechargeView == nil) {
        _rechargeView = [[UIImageView alloc] initWithImage:kGetImage(@"mine_cz")];
        _rechargeView.contentMode = UIViewContentModeScaleAspectFill;
        _rechargeView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toMember)];
        [_rechargeView addGestureRecognizer:tap];
    }
    return _rechargeView;
}

- (SDCycleScrollView *)bannerView {
    if (_bannerView == nil) {
        _bannerView = [[SDCycleScrollView alloc] initWithFrame:CGRectZero];
        _bannerView.delegate = self;
        _bannerView.showPageControl = NO;
        _bannerView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
        _bannerView.backgroundColor = kHEXA(@"FFFFFF", 0);
        _bannerView.pageDotColor = UIColor.whiteColor;
        _bannerView.currentPageDotColor = kAppMainColor;
        _bannerView.autoScrollTimeInterval = 5.0;
        _bannerView.pageControlDotSize = CGSizeMake(12, 12);
    }
    return _bannerView;
}

- (void)toMember {
    XMMemberViewController *vc = [[XMMemberViewController alloc] initWithNibName:kStringFromClass(XMMemberViewController) bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)toRecharge {
    XMRechargeViewController *vc = [[XMRechargeViewController alloc] initWithNibName:kStringFromClass(XMRechargeViewController) bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}


- (NSMutableArray *)bannerArray {
    if (!_bannerArray) {
        _bannerArray = [NSMutableArray array];
    }
    return _bannerArray;
}

#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    XMBannerModel *model = [self.bannerArray safe_objectAtIndex:index];
    if (![NSString isEmpty:model.link]) {
        XMWebViewController *vc = [[XMWebViewController alloc] init];
        vc.titleStr = model.title;
        vc.urlStr = model.link;
        vc.needNav = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

@end
